package com.zj.sixtyDayChallenge.list;

/**
 * @program: algorithm
 * @description: 删除链表的倒数第 N 个结点
 * 19 remove-nth-node-from-end-of-list
 * @author: Zhang Bo
 * @create: 2023-02-10 19:33:12
 **/
public class RemoveNthNodeFromEndOfList {
    public static void main(String[] args) {
        Solution solution = new RemoveNthNodeFromEndOfList().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {

            ListNode newHead = new ListNode(-1);
            ListNode n1 = newHead;
            ListNode n2 = newHead;
            newHead.next = head;

            for (int i = 0; i < n; i++) {
                n2 = n2.next;
            }
            while (n2.next != null) {

                n1 = n1.next;

                n2 = n2.next;
            }
            n1.next = n1.next.next;
            return newHead.next;

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}